<?php

    global $mikron;

    $sitemap = new sitemap_class();
    $mikron->OperatingInstructions->AddFunction('MIKRON_SITEMAP', 'OperatingInstruction_SiteMap', $sitemap);

    class sitemap_class
    {

        function OperatingInstruction_SiteMap($parameters = null)
        {
            global $mikron;
            $depth = 999;
            $start_depth = 0;
            $classes = Array();
            if(array_key_exists('class', $parameters))
            {
                $classes = explode(',', $parameters['class']);
            }
            if(array_key_exists('depth', $parameters))
            {
                $depth = (int)$parameters['depth'];
            }
            $menu = $mikron->Tools->SiteMenu->getSiteMenu();
            $this->DrawMenu($menu, $start_depth, $depth, $classes);
        }

        private function DrawMenu($menu, $start_depth, $depth, & $classes)
        {
            if($start_depth >= $depth)
            {
                return;
            }
            if(count($menu) < 1)
            {
                return;
            }
            echo '<ul>';
            foreach($menu as $menuitem)
            {
                $title = $menuitem->title;
                $address = $menuitem->address;
                $current = $menuitem->current;
                if($current)
                {
                    echo sprintf('<li><a id="current" href="%s">%s</a>', $address, $title);
                }
                else
                {
                    echo sprintf('<li><a href="%s">%s</a>', $address, $title);
                }
                $this->DrawMenu($menuitem->items, $start_depth + 1, $depth, $classes);
                echo '</li>';
            }
            echo '</ul>';
        }

    }

?>